/**
 * Created by ltx on 2017/9/28.
 */
(function () {
    //找url里面的id咯
    var article_id=getQueryString("id");
    console.log(article_id);

    articeShows(article_id);

    nlike(article_id);
    //seenum(article_id);

    subMessage(article_id);
})()
//发送反馈消息
function subMessage(article_id) {

    $('.sub').on('click',function () {
        var messa=$('.message').find('[name="descript"]').val();
        var userna = sessionStorage.getItem('username');
        console.log(messa);
        if (messa){
            $.ajax({
                type: 'POST',
                url: "/user/message",
                data: {
                    article_id:article_id,
                    message:messa,
                    username:userna
                },
                success: function () {
                    console.log('成功');
                }
            });
        }
        $('.message').hide();
    })
}
//喜欢的人数
function nlike(article_id) {
    //var like=$('.tools>.like>i').value;
    $('.like').on('click',function () {
        $.ajax({
            type: 'POST',
            url: "/user/nlike",
            data: {
                article_id:article_id
            },
            success: function () {
                console.log('成功');
            }
        });
    })
}
//看的人数
/*
function seenum(article_id) {
    //var like=$('.tools>.like>i').value;
    $('.see').on('click',function () {
        $.ajax({
            type: 'POST',
            url: "/user/seenum",
            data: {
                article_id:article_id
            },
            success: function () {
                console.log('成功');
            }
        });
    })
}
*/

//文章在页面展示
function articeShows(article_id) {
    var aID=article_id;
    $.ajax({
        url:"/blog",
        type:'GET',
        dataType:'JSON',
        data:{
            article_id:article_id
        },
        success:function (text) {
            //文章放置在页面上
            //console.log(text);
            //通过传的id值循环匹配到数据里面的那一项，
            //然后放到页面

            for (var i=0;i<text.length;i++){
                if (text[i].ID==aID){
                    //console.log(text[i].content);
                    var text1 = text[i].content;
                    var converter = new showdown.Converter();
                    var html = converter.makeHtml(text1);
                    document.getElementsByClassName('marked')[0].innerHTML = html;

                    $('.like').html(text[i].nlike);
                    var btime=text[i].post_time.split('T');
                    $('.time').html(btime[0]);
                    $('.see').html(text[i].custompeople);
                    console.log(text[i]);
                }
            }
        },
        error:function () {
            console.log('木有找到这篇文章的id');
        }
    });
}

//获取url信息，找id值咯
function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }else{
        return null;
    }
}
